package com.kaxicore.kaxi.controller;

import com.kaxicore.kaxi.entity.Staff;
import com.kaxicore.kaxi.httpResult.Result;
import com.kaxicore.kaxi.httpResult.ResultGenerator;
import com.kaxicore.kaxi.serviceImpl.StaffServiceImpl;
import com.kaxicore.kaxi.utils.PageRequest;
import com.kaxicore.kaxi.utils.PageResult;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
public class StaffController {
    @Autowired
    private StaffServiceImpl staffServiceImpl;


    @GetMapping("/staff")
    public Result<PageResult> getAllStaffs(@RequestParam int pageNum, @RequestParam int pageSize, @RequestParam String name) throws Exception {
        PageRequest pageRequest = new PageRequest(pageNum, pageSize);
        return ResultGenerator.genSuccessResult(staffServiceImpl.getAllStaffs(name, pageRequest));
    }

    @GetMapping("/staff/{id}")
    public Result getStaffById(@PathVariable Integer id) throws Exception {
        return ResultGenerator.genSuccessResult(staffServiceImpl.getStaffById(id));
    }

    @PostMapping("/staff")
    @ResponseBody
    public Result addStaff(Staff staff) throws Exception {
        staffServiceImpl.addStaff(staff);
        return ResultGenerator.genSuccessResult("员工信息添加成功！");
    }

    @PutMapping("/staff")
    @ResponseBody
    public Result updateStaff(Staff staff) throws Exception {
        staffServiceImpl.updateStaff(staff);
        System.out.println(staff);
        return ResultGenerator.genSuccessResult("员工信息编辑成功！");
    }

    @Delete("/staff/{id}")
    public void deleteStaff(@PathVariable Integer id) throws Exception {
        staffServiceImpl.deleteStaffById(id);
        return;
    }
}
